//
//  UrlImageButton.m
//  test image
//
//  Created by Xuyan Yang on 8/06/10.
//  Copyright 2010 __MyCompanyName__. All rights reserved.
//

#import "UrlImageView.h"
#import "UrlImageButton.h"
#import "NSURLAdditions.h"
#import "SDImageCache.h"
#import "UIImageView+WebCache.h"

//#import "NSURLAdditions.h"
#import "SDWebImageManager.h"
//#import "Macro.h"

@interface UIImage (scale)  

-(UIImage*)scaleToSize:(CGSize)size;  

@end  

@implementation UIImage (scale)  

-(UIImage*)scaleToSize:(CGSize)size  
{  
    // 创建一个bitmap的context  
    // 并把它设置成为当前正在使用的context  
    UIGraphicsBeginImageContext(size);  
	
    // 绘制改变大小的图片  
    [self drawInRect:CGRectMake(0, 0, size.width, size.height)];  
	
    // 从当前context中创建一个改变大小后的图片  
    UIImage* scaledImage = UIGraphicsGetImageFromCurrentImageContext();  
	
    // 使当前的context出堆栈  
    UIGraphicsEndImageContext();  
	
    // 返回新的改变大小后的图片  
    return scaledImage;  
} 

@end  

@interface UrlImageView()

-(NSURL*)getChangedURL:(NSURL*)url;
-(void)initArray;

@end

@implementation UrlImageView
@synthesize gridentAnimation;

@synthesize iconIndex;
@synthesize _animated;
- (id)initWithFrame:(CGRect)frame {
    if (self = [super initWithFrame:frame]) {
		iconIndex = -1;
        frame_final=frame;
		isScale= NO;
		_animated = YES;
		scaleSize = CGSizeZero;
        self.contentMode = UIViewContentModeRedraw;
        
        UISwipeGestureRecognizer *recognizer = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:nil];
        [self removeGestureRecognizer:recognizer];
        [recognizer release];
        
        UILongPressGestureRecognizer *recognizer1 = [[UILongPressGestureRecognizer alloc] initWithTarget:self action:nil];
        [self removeGestureRecognizer:recognizer1];
        [recognizer1 release];
        
        [self initArray];
	}
    return self;
}

- (void)dealloc {
    [sizeArray release];
    sizeArray = nil;
    [sizeDestArray release];
    sizeDestArray = nil;
    [gridentAnimation release];
    gridentAnimation = nil;

	[super dealloc];
}

-(void)initArray
{
    if (!sizeArray) {
      //  sizeArray = [NSArray arrayWithObjects:@"ZoomImage",@"DetailImage",@"BigImage",@"NormalImage",@"ViewImage",@"SmallImage",@"ColorImage",@"ShowCaseImage",@"GridImage",@"LargeImage", nil];
        sizeArray = [[NSArray alloc] initWithObjects:@"ZoomImage",@"DetailImage",@"BigImage",@"NormalImage",@"ViewImage",@"SmallImage",@"ColorImage",@"ShowCaseImage",@"GridImage",@"LargeImage", nil];
    }
    
    if (!sizeDestArray) {
      //  sizeDestArray = [NSArray arrayWithObjects:@"ZoomImage", @"ZoomImage", @"DetailImage", @"LargeImage", @"BigImage", @"ViewImage", @"SmallImage", @"GridImage", @"LargeImage", @"ZoomImage", nil];
        sizeDestArray = [[NSArray alloc] initWithObjects:@"ZoomImage", @"ZoomImage", @"DetailImage", @"LargeImage", @"BigImage", @"ViewImage", @"SmallImage", @"GridImage", @"LargeImage", @"ZoomImage", nil];
    }
}

-(UIImage*) getDefaultImage
{
	CGSize frameSize = self.frame.size;
    if(frameSize.width == 48 && frameSize.height == 48){
		return [UIImage  imageNamed:@"img_load_48x48.png"];
	}
	else if(frameSize.width == 73 && frameSize.height == 73){
		return [UIImage  imageNamed:@"img_load_73x73.png"];
	}
	else if(frameSize.width == 140 && frameSize.height == 140){
		return [UIImage  imageNamed:@"img_load_140x140.png"];
	}
	else if(frameSize.width == 160 && frameSize.height == 160){
		return [UIImage  imageNamed:@"img_load_160x160.png"];
	}
    else if(frameSize.width ==310 && frameSize.height == 310){
		return [UIImage  imageNamed:@"img_load_310x310.png"];
	}
	return nil;
}

-(NSURL*)getChangedURL:(NSURL*)url
{
    NSMutableString *strUrl = [NSMutableString stringWithString:url.absoluteString];
    if (!isRetina) {
        return url;
    }
    //去除ipod
//    NSString *strModel = [[[UIDevice currentDevice] model] lowercaseString];
//    if ([strModel rangeOfString:@"ipod"].length > 0) {
//        return url;
//    }
    if (![strUrl hasPrefix:@"http://"]) {
        return url;
    }
    NSArray *array = [strUrl componentsSeparatedByString:@"/"];
    if ([array count] > 1) {
        NSString *strDest = [array objectAtIndex:[array count] - 2];
     //   LFLog(@"strDest:%@-sizeArray count:%d",strDest, [sizeArray count]);
        if (!sizeArray) {
            [self initArray];
        }
        if ([sizeArray containsObject:strDest]) {
            NSRange range = [strUrl rangeOfString:strDest];
            int index = [sizeArray indexOfObject:strDest];
            [strUrl replaceCharactersInRange:range withString:[sizeDestArray objectAtIndex:index]];
        }
    }
    
    return (NSURL*)[NSURL URLWithString:strUrl];
}

- (void) setImageFromUrl:(BOOL)animated withUrl:(NSString *)iconUrl;
{
    if(iconUrl == nil || [iconUrl isKindOfClass:[NSNull class]]){
        return;
    }
	_animated = animated;
    
	NSURL* tempUrl = [NSURL URLWithString:[iconUrl stringByAddingPercentEscapesUsingEncoding:NSASCIIStringEncoding]];

	NSURL* finallyUrl = nil;
	if([NSURL isWebURL:tempUrl])
	{
		finallyUrl = tempUrl;
	}
	else {
	}

	[super setImageWithURL:finallyUrl placeholderImage:[self getDefaultImage]];
	
}	

- (void)setImageWithURL:(NSURL *)url
{
	[self setImageWithURL:url placeholderImage:[self getDefaultImage]];
}

- (void)setImageWithURL:(NSURL *)url placeholderImage:(UIImage *)placeholder
{
 //    self.image = placeholder;
	_animated=YES;
    if (url)
    {
        url = [self getChangedURL:url];
    }
    
    [super setImageWithURL:url placeholderImage:placeholder];
	isLoad = NO;
}

- (void)setImageWithURL:(NSURL *)url placeholderImage:(UIImage *)placeholder zoom:(BOOL)zoomStatus
{
    //    self.image = placeholder;
    _animated=YES;
    if (url)
    {
        url = [self getChangedURL:url];
    }
    
    [super setImageWithURL:url placeholderImage:placeholder zoom:zoomStatus];
    isLoad = NO;
}

- (void)cancelCurrentImageLoad
{
	[[SDWebImageManager sharedManager] cancelForDelegate:self];
}

- (BOOL)isLoad
{
    return isLoad;
}

-(void)scaleToSize:(CGSize)size 
{
	isScale= YES;
	scaleSize =size;
	UIImage* newImage = [self.image scaleToSize:size];
	self.image = newImage;
}

@end
